package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.TbBuildingMapper;
import com.hqyj.dao.TbCommunityMapper;
import com.hqyj.dao.TbHouseMapper;
import com.hqyj.dao.TbOwnerMapper;
import com.hqyj.entity.SearchBean;
import com.hqyj.pojo.TbCommunity;
import com.hqyj.pojo.TbHouse;
import com.hqyj.pojo.TbOwner;
import com.hqyj.service.TbOwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 业主表 服务实现类
 * </p>
 *
 * @author gp
 * @since 2021-09-26
 */
@Service
public class TbOwnerServiceImpl implements TbOwnerService {

    @Autowired
    private TbOwnerMapper tbOwnerMapper;

    @Autowired
    private TbHouseMapper tbHouseMapper;

    @Autowired
    private TbBuildingMapper tbBuildingMapper;



    @Override
    public HashMap<String, Object> tOList(SearchBean searchBean) {
        HashMap<String,Object> map = new HashMap<>();
        //第一步:创建分页对象
        Page<TbOwner> userInfoPage = new Page<>(searchBean.getCurrentPage(),searchBean.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(searchBean.getDirection() != null && !searchBean.getDirection().equals(""),
                "name",
                      searchBean.getDirection()
        );
        queryWrapper.select("id","community_id","house_id","name","idcard","telephone","picture","email","profession","sex","type","remark","birthday");
        //第二部:查询分页，获取分页数据集
        Page<TbOwner> pageList = tbOwnerMapper.selectPage(userInfoPage, queryWrapper);
        List<TbOwner> records = pageList.getRecords();
        records.forEach(s->{
          s.setTbCommunity(tbOwnerMapper.getByCommunityId(s.getCommunityId()));
          s.setTbBuilding(tbBuildingMapper.selectById(s.getHouseId()));
        });

        //获取当前页的集合
        map.put("info",records);
        //获取总条数
        map.put("total",pageList.getTotal());
        //获取总页数
        map.put("totalPage",pageList.getPages());
        return map;
    }

    @Override
    @Transactional
    public HashMap<String, Object> insertTO(TbOwner tbOwner) {
        Random random = new Random();
        TbHouse tbHouse = new TbHouse();
        int count = tbOwnerMapper.insert(tbOwner);
        TbCommunity community = tbOwnerMapper.getByCommunityId(tbOwner.getCommunityId());
        String nameById = tbBuildingMapper.getNameById(tbOwner.getHouseId());
        tbHouse.setCommunityName(community.getName());
        tbHouse.setCommunityId(tbOwner.getCommunityId());
        tbHouse.setBuildingName(nameById);
        tbHouse.setBuildingId(tbOwner.getHouseId());
        tbHouse.setName(community.getName() + nameById);
        tbHouse.setCode("TH" + random.nextInt(50000));
        tbHouse.setOwnerId(tbOwner.getId());
        tbHouse.setOwnerName(tbOwner.getName());
        tbHouse.setTelephone(tbOwner.getTelephone());
        tbHouse.setRoomNum(random.nextInt(10));
        tbHouse.setUnit(random.nextInt(5));
        tbHouse.setFloor(random.nextInt(6));
        tbHouseMapper.insert(tbHouse);
        HashMap<String,Object> hashMap = new HashMap<>();

        if (count > 0){
            hashMap.put("info","添加成功");
            //添加楼房信息
            //添加车辆信息
        }else{
            hashMap.put("info","登录失败");
        }
        return hashMap;
    }

    @Override
    @Transactional
    public HashMap<String, Object> deleteTO(int id) {
        HashMap<String,Object> map = new HashMap<>();
        TbHouse house = tbHouseMapper.getHouseByOwnerId(id);
        if (house != null){
            tbHouseMapper.deleteById(house.getId());
        }
        int count = tbOwnerMapper.deleteById(id);
        if (count > 0){
            map.put("info","删除成功");
            //删除对应房屋信息
            //删除对应车位信息
        }else{
            map.put("info","删除失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> updateTO(TbOwner tbOwner) {
        HashMap<String,Object> hashMap = new HashMap<>();
        int count = tbOwnerMapper.updateById(tbOwner);
        if (count > 0){
            hashMap.put("info","修改成功");
            //更新对应的房屋信息
            //更新对应得车位信息
        }else{
            hashMap.put("info","修改失败");
        }
        return hashMap;
    }

    @Override
    public HashMap<String, Object> findById(int id) {
        TbOwner tbOwner = tbOwnerMapper.findById(id);
        HashMap<String,Object> map = new HashMap<>();
        map.put("info",tbOwner);
        return map;
    }

    @Override
    public HashMap<String, Object> login(TbOwner tbOwner) {
        HashMap<String, Object> map = new HashMap<>();
        TbOwner owner = tbOwnerMapper.login(tbOwner);
        if (owner == null) {
            map.put("info", "无此账号");
        } else {
            map.put("info", "登录成功");
        }
        return map;
    }


}
